package com.yunsc.common.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-05-09 8:49
 */


public class TimesToDateSerializer extends JsonSerializer<Integer> {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");

    @Override
    public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException {
        if (value == null || value == 0) {
            gen.writeNull();
            return;
        }
        long milliseconds = value * 1000L;
        Date date = new Date(milliseconds); // 时间戳是以秒为单位，Date默认是毫秒
        String formattedDate = DATE_FORMAT.format(date);
        gen.writeString(formattedDate);
    }
}
